package com.avic.tools.config;

import com.avic.tools.config.bean.AvicConfigElement;
import com.avic.utils.FileUtil;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.MultipartConfigElement;
import java.io.File;

/**
 * @author https://blog.csdn.net/llibin1024530411/article/details/79474953
 */
@Configuration
public class MultipartConfig {

    public String SEP = File.separator;
    public String location = System.getProperty("user.home") + SEP + ".ehs" + SEP + "file" + SEP + "tmp";


    /**
     * 文件上传临时路径
     */
    @Bean
    AvicConfigElement avicConfigElement() {
        String equipTERuleLocation = location.concat(SEP + "equipTERule");
        // 默认尾斜线
        String avatarLocation = location.concat(SEP + "avatar" + SEP);
        String localFiles = location.concat(SEP + "localFiles");
        String external = location.concat(SEP + "external");
        // 自动创建规则图目录以及头像目录
        FileUtil.mkdirs(equipTERuleLocation);
        FileUtil.mkdirs(avatarLocation);
        FileUtil.mkdirs(localFiles);
        FileUtil.mkdirs(external);
        /**
         * 依照情况而定
         */
        AvicConfigElement avicConfigElement = new AvicConfigElement();
        avicConfigElement.setEquipTERuleLocation(equipTERuleLocation);
        avicConfigElement.setAvatarLocation(avatarLocation);
        avicConfigElement.setLocalFilesLocation(localFiles);
        avicConfigElement.setLocation(location);
        return avicConfigElement;
    }

    /**
     * 文件上传临时路径
     *
     * @return
     */
    @Bean
    MultipartConfigElement multipartConfigElement() {
        /**
         * 临时目录
         */
        MultipartConfigFactory factory = new MultipartConfigFactory();
        FileUtil.mkdirs(location);
        factory.setLocation(location);
        return factory.createMultipartConfig();
    }
}
